﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace Logistics.Utility
{
    public class EnumOperate
    {
        /// <summary>
        /// 获取枚举的描述信息
        /// 添加人：季健国
        /// 添加时间：2013-12-02
        /// </summary>
        /// <param name="e">传入枚举对象</param>
        /// <returns>得到对应描述信息</returns>
        public static String GetEnumDesc(System.Enum e)
        {
            var enumInfo = e.GetType().GetField(e.ToString());
            var enumAttributes = (DescriptionAttribute[])enumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return enumAttributes.Length > 0 ? enumAttributes[0].Description : e.ToString();
        }

        /// <summary>
        /// 获取枚举的描述信息，泛型方法
        /// 添加人：季健国
        /// 添加时间：2013-12-02
        /// </summary>
        /// <typeparam name="T">任意枚举类型</typeparam>
        /// <param name="t">传入枚举泛型</param>
        /// <returns>得到对应描述</returns>
        public static string GetEnumDesc<T>(T t)
        {
            var enumInfo = t.GetType().GetField(t.ToString());
            if (enumInfo == null)
            {
                return null;
            }
            var enumAttributes = (DescriptionAttribute[])enumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return enumAttributes.Length > 0 ? enumAttributes[0].Description : t.ToString();
        }

        public static string GetName(Type t, object v)
        {
            try
            {
                return System.Enum.GetName(t, v);
            }
            catch
            {
                return "未知"; //UNKNOWN
            }
        }


        /// <summary>
        /// 返回指定枚举类型的指定值的描述
        /// 添加人：季健国
        /// 添加时间：2013-12-02
        /// </summary>
        /// <param name="t">枚举类型</param>
        /// <param name="v">枚举值</param>
        /// <returns></returns>
        public static string GetDescription(Type t, object v)
        {
            try
            {
                var fi = t.GetField(GetName(t, v));
                var attributes =
                    (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch
            {
                return "未知";
            }
        }

        /// <summary>
        /// 将指定枚举类型转换成List，用来绑定ListControl
        /// 添加人：季健国
        /// 添加时间：2013-12-02
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="selectFeild">默认选择的项的值</param>
        /// <returns></returns>
        public static IEnumerable<ListItem> ConvertEnumToListItems(Type enumType)
        {
            if (enumType.IsEnum == false) { yield return null; }
            var typeDescription = typeof(DescriptionAttribute);
            var fields = enumType.GetFields();
            foreach (var field in fields.Where(field => !field.IsSpecialName))
            {
                var strValue = field.GetRawConstantValue().ToString();
                var arr = field.GetCustomAttributes(typeDescription, true);
                var strText = arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : field.Name;
                var item = new ListItem { Text = strText, Value = strValue };
                yield return item;
            }
        }

        /// <summary>
        /// 将指定枚举类型转换成List，用来绑定ListControl
        /// 添加人：季健国
        /// 添加时间：2013-12-02
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="selectFeild">默认选择的项的值</param>
        /// <returns></returns>
        public static IEnumerable<ListItem> ConvertEnumToListItems(Type enumType, string selectFeild)
        {
            if (enumType.IsEnum == false) { yield return null; }
            var typeDescription = typeof(DescriptionAttribute);
            var fields = enumType.GetFields();
            foreach (var field in fields.Where(field => !field.IsSpecialName))
            {
                var strValue = field.GetRawConstantValue().ToString();
                var arr = field.GetCustomAttributes(typeDescription, true);
                var strText = arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : field.Name;
                var item = new ListItem { Text = strText, Value = strValue };
                if (strValue.Equals(selectFeild))
                    item.Selected = true;
                yield return item;
            }
        }


        /// <summary>
        /// 将指定枚举类型转换成List，用来绑定ListControl(作用于枚举值不是数字的使用)
        /// 添加人：沈梦君
        /// 添加时间：2014-01-21
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns></returns>
        public static IEnumerable<ListItem> ConvertEnumNameToListItems(Type enumType)
        {
            if (enumType.IsEnum == false) { yield return null; }
            var typeDescription = typeof(DescriptionAttribute);
            var fields = enumType.GetFields();
            foreach (var field in fields.Where(field => !field.IsSpecialName))
            {
                var strValue = field.Name;
                var arr = field.GetCustomAttributes(typeDescription, true);
                var strText = arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : field.Name;
                var item = new ListItem { Text = strText, Value = strValue };
                yield return item;
            }
        }

        /// <summary>
        /// 张金伟
        /// 2014-1-26
        /// 获取enum定义的描述
        /// </summary>
        /// <param name="enumType">类型</param>
        /// <param name="strvalue">枚举项的Name</param>
        /// <returns></returns>
        public static string GetNameByValue(System.Type enumType, string strvalue)
        {
            if (strvalue == null || strvalue.Trim() == "")
            {
                return "";
            }

            System.Reflection.FieldInfo[] fields = enumType.GetFields();

            //检索所有字段
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum == true && field.Name == strvalue.Trim())
                {
                    //枚举描述
                    DescriptionAttribute Des = null;
                    //枚举中文描述
                    object[] aryDes = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (aryDes.Length > 0)
                    {
                        Des = aryDes[0] as DescriptionAttribute;
                    }
                    if (Des != null)
                        return Des.Description;
                }
            }
            //默认返回值
            return strvalue;
        }

        /// <summary>
        /// 张金伟
        /// 2014-1-26
        /// 根据枚举值获得描述
        /// </summary>
        /// <param name="enumtype">类型</param>
        /// <param name="value">枚举项的值</param>
        /// <returns></returns>
        public static string GetValueByEnumName(Type enumtype, int value)
        {
            string enumname = string.Empty;
            System.Reflection.FieldInfo[] fields = enumtype.GetFields();
            //检索所有字段
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum == true)
                {
                    //枚举英文
                    string name = field.Name;
                    if ((int)System.Enum.Parse(enumtype, name, true) == value)
                    {
                        DescriptionAttribute da = null;
                        object[] arrobj = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
                        if (arrobj.Length > 0)
                        {
                            da = arrobj[0] as DescriptionAttribute;
                        }
                        if (da != null)
                        {
                            //枚举中文描述
                            enumname = da.Description;
                        }
                    }
                }
            }
            return enumname;
        }


        /// 根据枚举值获得变量
        /// </summary>
        /// <param name="enumtype">类型</param>
        /// <param name="value">枚举项的值</param>
        /// <returns></returns>
        public static string GetVariableByValue(Type enumtype, int value)
        {
            string enumname = string.Empty;
            System.Reflection.FieldInfo[] fields = enumtype.GetFields();
            //检索所有字段
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum == true)
                {
                    //枚举英文
                    string name = field.Name;
                    if ((int)System.Enum.Parse(enumtype, name, true) == value)
                    {
                        return field.Name;
                    }
                }
            }
            return "";
        }

    }
}
